/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.admin.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.admin.dto.MaintainTimeDTO;
import org.springblade.modules.admin.dto.MaintainTimeSubmitDTO;
import org.springblade.modules.admin.entity.*;
import org.springblade.modules.admin.enums.ContractStatusEnum;
import org.springblade.modules.admin.service.IBuildingContractPlusService;
import org.springblade.modules.admin.service.IMaintainContractTypeService;
import org.springblade.modules.admin.vo.ContractLogVO;
import org.springblade.modules.admin.mapper.ContractLogMapper;
import org.springblade.modules.admin.service.IContractLogService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.modules.admin.vo.contract.*;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.sql.Struct;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author ysq
 * @since 2025-02-11
 */
@Service
@Slf4j
public class ContractLogServiceImpl extends BaseServiceImpl<ContractLogMapper, ContractLog> implements IContractLogService {

	@Autowired
	private IUserService userService;

	@Autowired
	private IMaintainContractTypeService maintainContractTypeService;

	/**
	 * @param type 操作类型
	 *             101:系统触发-待续签
	 *             102:系统触发-即将到期
	 *             103:系统触发-待生效
	 *             104:系统触发-已生效
	 *             105:系统触发-已到期
	 *             201:人为操作-提交
	 *             202:人为操作-保存草稿
	 *             203:人为操作-提交到草稿以后的编辑-重新提交审核
	 *             204:人为操作-审核通过后的查看提交附件
	 *             205:点击-提交草稿箱后编辑提交
	 *             301:列表按钮操作-终止
	 *             302:列表按钮操作-作废
	 *             303:列表按钮操作-续签
	 *             304:列表按钮操作-脱保
	 *             304:列表按钮操作-脱保
	 *             305点击-取消脱保
	 *             306点击-审核通过
	 *             310 点击-转交审批
	 *             405:编辑后重新提交
	 */
	@Override
	public void saveLogHistory(Integer type, BuildingContractPlus oldBuildingContractPlus, BuildingContractPlus buildingContractPlus) {
		ContractLog contractLog = new ContractLog();
		ContractHistoryVo historyInfo = new ContractHistoryVo();
		String userName = AuthUtil.getNickName();
		userName = userName == "" ? "系统触发" : userName;
		contractLog.setCreateUserName(userName);
		contractLog.setContractId(oldBuildingContractPlus == null ? buildingContractPlus.getId() : oldBuildingContractPlus.getId());
		//操作状态
		ContractStatusVo beforeStatusInfo = new ContractStatusVo();
		ContractStatusVo afterStatusInfo = new ContractStatusVo();

		//操作内容
		ContractUpdateDescVo beforeDesc = new ContractUpdateDescVo();
		ContractUpdateDescVo afterDesc = new ContractUpdateDescVo();

		List<String> beforeUpdateDescs = new ArrayList<>(16);
		List<String> afterUpdateDescs = new ArrayList<>(16);

		if (type == 101) {
			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(Convert.toBool(oldBuildingContractPlus.getDisDeleted(), false) ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName("待续签");
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));
			historyInfo.setClickName("系统触发-待续签");
			beforeUpdateDescs.add("系统触发->待续签-无内容变更");
			afterUpdateDescs.add("系统触发->待续签-无内容变更");


		} else if (type == 102) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName("即将到期");
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));
			historyInfo.setClickName("系统触发-即将到期");

			beforeUpdateDescs.add("系统触发->即将到期-无内容变更");
			afterUpdateDescs.add("系统触发->即将到期-无内容变更");
		} else if (type == 103) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getCheckName(oldBuildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName("待生效");

			historyInfo.setClickName("系统触发-待生效");

			beforeUpdateDescs.add("系统触发->待生效-无内容变更");
			afterUpdateDescs.add("系统触发->待生效-无内容变更");
		} else if (type == 104) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName("已生效");

			historyInfo.setClickName("系统触发-已生效");

			beforeUpdateDescs.add("系统触发->已生效-无内容变更");
			afterUpdateDescs.add("系统触发->已生效-无内容变更");
		} else if (type == 105) {
			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName("已到期");
			historyInfo.setClickName("系统触发-已到期");

			beforeUpdateDescs.add("系统触发->已到期-无内容变更");
			afterUpdateDescs.add("系统触发->已到期-无内容变更");
		} else if (type == 201) {

			afterStatusInfo.setDraftsName(buildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName(getCheckName(buildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));

			//内容变更
			beforeUpdateDescs.add("仅提交审核，右侧为初始合同内容");
			ContractUpdateDescVo contractUpdateDescVo = addContractSaveLog(buildingContractPlus);
			afterUpdateDescs.addAll(contractUpdateDescVo.getUpdateDescs());
			historyInfo.setClickName("点击-提交");
		} else if (type == 202) {
			beforeUpdateDescs.add("草稿箱不记录变更内容");
			afterUpdateDescs.add("草稿箱不记录变更内容");
			afterStatusInfo.setDraftsName("草稿箱");
			historyInfo.setClickName("点击-保存草稿");
		} else if (type == 203) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			beforeUpdateDescs.add("仅提交审核，右侧为初始合同内容");
			afterStatusInfo.setDraftsName(buildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName("待审核");
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));
			ContractUpdateDescVo contractUpdateDescVo = addContractSaveLog(buildingContractPlus);
			List<String> updateDescs = contractUpdateDescVo.getUpdateDescs();
			afterUpdateDescs.addAll(updateDescs);
			historyInfo.setClickName("点击-提交到草稿-重新提交审核");
		} else if (type == 205) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(Convert.toStr(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()), "-"));

			afterStatusInfo.setDraftsName(buildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getStatus()));
			afterStatusInfo.setCheckName(getCheckName(buildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(Convert.toStr(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()), "-"));


			beforeUpdateDescs.add("草稿箱不记录变更内容");
			afterUpdateDescs.add("草稿箱不记录变更内容");

			historyInfo.setClickName("点击-提交草稿箱后编辑提交");

		} else if (type == 301) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(Convert.toBool(oldBuildingContractPlus.getDisDeleted(), false) ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName("-");
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName("已终止");

			historyInfo.setClickName("点击-终止");

			beforeUpdateDescs.add("点击-终止-无内容变更");
			afterUpdateDescs.add("点击-终止-无内容变更");
		} else if (type == 302) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName("作废");
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			historyInfo.setClickName("点击-作废");

			beforeUpdateDescs.add("点击-作废-无内容变更");
			afterUpdateDescs.add("点击-作废-无内容变更");
		} else if (type == 303) {
			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName("已续签");
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			historyInfo.setClickName("点击-续签");
			beforeUpdateDescs.add("点击-续签-无内容变更");
			afterUpdateDescs.add("点击-续签-无内容变更");

		} else if (type == 304) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(Convert.toBool(oldBuildingContractPlus.getDisDeleted(), false) ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName("脱保");
			afterStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			historyInfo.setClickName("点击-脱保");

			beforeUpdateDescs.add("点击-脱保-无内容变更");
			afterUpdateDescs.add("点击-脱保-" + getTuoTypeName(buildingContractPlus.getTuoType(), buildingContractPlus.getTuoTypeDesc()) + "-无内容变更");
		} else if (type == 305) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(buildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName(getCheckName(buildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));

			historyInfo.setClickName("点击-取消脱保");
			beforeUpdateDescs.add("点击-取消脱保-无内容变更");
			afterUpdateDescs.add("点击-取消脱保-无内容变更");
		} else if (type == 306) {

			beforeStatusInfo.setDraftsName("-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName("-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName("审核通过");
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));


			//内容变更前
			beforeUpdateDescs.add("审核备注:-");
			//内容变更后
			afterUpdateDescs.add("审核备注:" + buildingContractPlus.getStatusDesc());
			historyInfo.setClickName("点击-审核通过");
		} else if (type == 307) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(buildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName(getCheckName(buildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));

			historyInfo.setClickName("点击-取消终止");

			beforeUpdateDescs.add("点击-取消终止-无内容变更");
			afterUpdateDescs.add("点击-取消终止-无内容变更");
		} else if (type == 308) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(buildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName(getCheckName(buildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));

			historyInfo.setClickName("点击-取消作废");

			beforeUpdateDescs.add("点击-取消作废-无内容变更");
			afterUpdateDescs.add("点击-取消作废-无内容变更");
		} else if (type == 309) {

			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName("待审核");
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName("审核未通过");
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));


			//内容变更前
			beforeUpdateDescs.add("审核备注:-");
			//内容变更后
			afterUpdateDescs.add("审核备注:" + buildingContractPlus.getStatusDesc());
			historyInfo.setClickName("点击-审核未通过");
		} else if (type == 310) {

			beforeStatusInfo.setDraftsName(oldBuildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
			beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
			beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

			afterStatusInfo.setDraftsName(buildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
			afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
			afterStatusInfo.setCheckName(getCheckName(buildingContractPlus.getStatus()));
			afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));
			historyInfo.setClickName("点击-转交审核");
			User user = userService.getById(Convert.toLong(oldBuildingContractPlus.getCheckUserId(), -1L));
			if (user == null) {
				beforeUpdateDescs.add("审核人-");
			} else {
				beforeUpdateDescs.add("审核人-" + user.getRealName());
			}
			afterUpdateDescs.add("审核人-" + userService.getById(buildingContractPlus.getCheckUserId()).getRealName());
		}
		beforeDesc.setUpdateDescs(beforeUpdateDescs);
		afterDesc.setUpdateDescs(afterUpdateDescs);
		historyInfo.setUpdateType(type);
		contractLog.setHistoryInfo(historyInfo);
		contractLog.setAfterDesc(afterDesc);
		contractLog.setBeforeDesc(beforeDesc);
		contractLog.setBeforeStatusInfo(beforeStatusInfo);
		contractLog.setAfterStatusInfo(afterStatusInfo);
		this.save(contractLog);
	}

	@Override
	public void saveLog(BuildingContractPlus oldBuildingContractPlus, BuildingContractPlus buildingContractPlus) {
		try {
			//第一步判断是否新增还是修改
			Boolean disDeleted = Convert.toBool(buildingContractPlus.getDisDeleted(), false);
			if (oldBuildingContractPlus == null) {
				if (disDeleted) {
					saveLogHistory(202, oldBuildingContractPlus, buildingContractPlus);
				} else {
					saveLogHistory(201, oldBuildingContractPlus, buildingContractPlus);
				}
			} else {
				if (oldBuildingContractPlus.getDisDeleted() && buildingContractPlus.getDisDeleted() == false) {
					saveLogHistory(203, oldBuildingContractPlus, buildingContractPlus);
				}
				if (disDeleted && oldBuildingContractPlus.getDisDeleted()) {
					saveLogHistory(205, oldBuildingContractPlus, buildingContractPlus);
				}

				buildingContractPlus.setStatus(Convert.toInt(buildingContractPlus.getStatus(), -1));
				//列表按钮操作-脱保
				if (Convert.toInt(buildingContractPlus.getContractExtStatus(), -1) == 4) {
					saveLogHistory(304, oldBuildingContractPlus, buildingContractPlus);
					return;
				}

				//列表按钮操作-终止
				if (buildingContractPlus.getStopTime() != null) {
					saveLogHistory(301, oldBuildingContractPlus, buildingContractPlus);
					return;
				}

				//列表按钮操作-作废
				if (Convert.toInt(buildingContractPlus.getContractExtStatus(), -1) == 5) {
					saveLogHistory(302, oldBuildingContractPlus, buildingContractPlus);
					return;
				}

				//列表按钮操作-续签
				if (Convert.toInt(buildingContractPlus.getContractExtStatus(), -1) == 2) {
					saveLogHistory(303, oldBuildingContractPlus, buildingContractPlus);
					return;
				}

				//点击审核通过
				if (buildingContractPlus.getStatus() == 2) {
					saveLogHistory(306, oldBuildingContractPlus, buildingContractPlus);
					return;
				}
				//点击审核未通过
				if (buildingContractPlus.getStatus() == 3) {
					saveLogHistory(309, oldBuildingContractPlus, buildingContractPlus);
				}

				//编辑更新:
				if (buildingContractPlus.getOldBuildingContractPlus() != null && !buildingContractPlus.getDisDeleted()) {
					this.updateContractSaveLog(buildingContractPlus.getOldBuildingContractPlus(), buildingContractPlus, buildingContractPlus.getUpdateType());
				}
			}
		} catch (Exception e) {
			log.info("添加合同日志报错.......");
		}
	}

	/**
	 * 1:服务不达标2:竞对签约3:客户暂停使用电梯-1:其他
	 *
	 * @param status    脱保类型
	 * @param otherDesc 其它描述
	 * @return
	 */
	private String getTuoTypeName(Integer status, String otherDesc) {
		status = Convert.toInt(status, -2);
		if (status == 1) {
			return "服务不达标";
		}
		if (status == 2) {
			return "竞对签约3";
		}
		if (status == 3) {
			return "客户暂停使用电梯";
		}
		if (status == -1) {
			return "其他-" + otherDesc;
		}
		return "-";
	}


	/**
	 * 合同扩展状态
	 * 1:待续签
	 * 2:已续签
	 * 3:即将到期
	 * 4:已脱保
	 * 5:已作废
	 *
	 * @param status
	 * @return
	 */
	private String getContractExtStatusName(Integer status) {
		status = Convert.toInt(status, -1);
		status = Convert.toInt(status, -1);
		if (status == 1) {
			return "待续签";
		}
		if (status == 2) {
			return "已续签";
		}
		if (status == 3) {
			return "即将到期";
		}
		if (status == 4) {
			return "已脱保";
		}
		if (status == 5) {
			return "已作废";
		}
		return "-";
	}

	/**
	 * 获取审核状态
	 * 状态1:未审核，2:审核通过，3审核未通过
	 *
	 * @param status
	 * @return
	 */
	private String getCheckName(Integer status) {
		status = Convert.toInt(status, -1);
		if (status == 1) {
			return "未审核";
		}
		if (status == 2) {
			return "审核通过";
		}
		if (status == 3) {
			return "审核未通过";
		}
		return "-";
	}


	/**
	 * 合同来源	1:新签	2:续签	3:转签	4:丢失	5:夺回	6:新梯移交	7:免保转签
	 */
	private String getContractSourceName(Integer status) {
		status = Convert.toInt(status, -1);
		if (status == 1) {
			return "新签";
		} else if (status == 2) {
			return "续签";
		} else if (status == 3) {
			return "转签";
		} else if (status == 4) {
			return "丢失";
		} else if (status == 5) {
			return "夺回";
		} else if (status == 6) {
			return "新梯移交";
		} else if (status == 7) {
			return "免保转签";
		} else {
			return "-";
		}
	}

	/**
	 * 庸与字段(用于列表刷选)	合同类型	1:保养合同	2:委托合同	3:质保合同	4:维系合同
	 */

	private String getContractTypeName(Integer status) {
		status = Convert.toInt(status, -1);
		if (status == 1) {
			return "保养合同";
		} else if (status == 2) {
			return "委托合同";
		} else if (status == 3) {
			return "质保合同";
		} else if (status == 4) {
			return "维修合同";
		} else {
			return "-";
		}
	}

	/**
	 * 1:支票	2:现金	3:电汇	4:成若电汇
	 *
	 * @param status
	 * @return
	 */
	private String getPayTypeName(Integer status) {
		status = Convert.toInt(status, -1);
		if (status == 1) {
			return "支票";
		} else if (status == 2) {
			return "现金";
		} else if (status == 3) {
			return "电汇";
		} else if (status == 4) {
			return "承兑电汇";
		} else {
			return "-";
		}
	}

	/**
	 * * 发票类型	1:增值税专用发票	2:增值税普通发票	3:收据	4:已收款未开票	5:已开收据未付款	6:已开收据未开发票	7：预支发票	8：其它
	 *
	 * @param status
	 * @return
	 */
	private String getInvoiceTypeName(Integer status) {
		if (status == 1) {
			return "增值税专用发票";
		} else if (status == 2) {
			return "增值税普通发票";
		} else if (status == 3) {
			return "收据";
		} else if (status == 4) {
			return "已收款未开票";
		} else if (status == 5) {
			return "已开收据未付款";
		} else if (status == 6) {
			return "已开收据未开发票";
		} else if (status == 7) {
			return "预支发票";
		} else {
			return "其它";
		}
	}

	/**
	 * 付款周期	1:周期结束时付款	2:周期中期时付款	3:周期开始时付款
	 */
	private String getPayDateTypeName(Integer status) {
		status = Convert.toInt(status, -1);
		if (status == 1) {
			return "周期结束时付款";
		} else if (status == 2) {
			return "周期中期时付款";
		} else if (status == 3) {
			return "周期开始时付款";
		} else {
			return "-";
		}
	}

	/**
	 * 保养人数 0-不限 1-2人 2-2人以上
	 */
	private String getMaintainPersonnelName(Integer status) {
		if (status == 0) {
			return "不限";
		} else if (status == 1) {
			return "2人";
		} else if (status == 2) {
			return "2人以上";
		} else {
			return "-";
		}
	}


	/**
	 * 支付方式	1:月结	2:两月结	3:季度结	4:四月结	5:半年结	6:年度结	7:预付50%	8:完结50%	9:一次性付款	10:免费
	 */
	private String getpayDateDescName(Integer status) {
		if (status == 1) {
			return "月结";
		} else if (status == 2) {
			return "两月结";
		} else if (status == 3) {
			return "季度结";
		} else if (status == 4) {
			return "四月结";
		} else if (status == 5) {
			return "半年结";
		} else if (status == 6) {
			return "年度结";
		} else if (status == 7) {
			return "预付50%";
		} else if (status == 8) {
			return "完结50%";
		} else if (status == 9) {
			return "一次性付款";
		} else if (status == 10) {
			return "免费";
		} else {
			return "-";
		}
	}


	/**
	 * 检费用类型1：包缴，2代缴,3:自缴
	 */
	private String getYearSpeedGovernorCostTypeName(Integer status) {
		if (status == 1) {
			return "包";
		} else if (status == 2) {
			return "代缴";
		} else if (status == 3) {
			return "自缴";
		} else {
			return "-";
		}
	}


	@Override
	public void updateContractSaveLog(BuildingContractPlus oldBuildingContractPlus, BuildingContractPlus
		buildingContractPlus, Integer type) {
		if (StrUtil.isEmpty(buildingContractPlus.getMaintainMainUserName())) {
			User user = userService.getById(buildingContractPlus.getMaintainMainUser());
			if (user != null) {
				buildingContractPlus.setMaintainMainUserName(user.getRealName());
			}
		}
		ContractLog contractLog = new ContractLog();
		ContractHistoryVo historyInfo = new ContractHistoryVo();
		historyInfo.setClickName(type == 1 ? "点击-编辑修改" : "重新提交审核");
		contractLog.setHistoryInfo(historyInfo);
		String userName = AuthUtil.getNickName();
		userName = userName == "" ? "系统触发" : userName;
		contractLog.setCreateUserName(userName);
		contractLog.setContractId(oldBuildingContractPlus == null ? buildingContractPlus.getId() : oldBuildingContractPlus.getId());

		ContractUpdateDescVo beforeDesc = new ContractUpdateDescVo();
		ContractUpdateDescVo afterDesc = new ContractUpdateDescVo();

		ContractStatusVo afterStatusInfo = new ContractStatusVo();
		ContractStatusVo beforeStatusInfo = new ContractStatusVo();
		beforeStatusInfo.setDraftsName(Convert.toBool(oldBuildingContractPlus.getDisDeleted(), false) ? "草稿箱" : "-");
		beforeStatusInfo.setContractExtStatusName(getContractExtStatusName(oldBuildingContractPlus.getContractExtStatus()));
		beforeStatusInfo.setCheckName(getCheckName(oldBuildingContractPlus.getStatus()));
		beforeStatusInfo.setContractStatusName(ContractStatusEnum.getName(oldBuildingContractPlus.getContractStatus()));

		afterStatusInfo.setDraftsName(buildingContractPlus.getDisDeleted() ? "草稿箱" : "-");
		afterStatusInfo.setContractExtStatusName(getContractExtStatusName(buildingContractPlus.getContractExtStatus()));
		afterStatusInfo.setCheckName(getCheckName(buildingContractPlus.getStatus()));
		afterStatusInfo.setContractStatusName(ContractStatusEnum.getName(buildingContractPlus.getContractStatus()));
		contractLog.setBeforeStatusInfo(beforeStatusInfo);
		contractLog.setAfterStatusInfo(afterStatusInfo);

		//内容变更前
		List<String> befoerUpdateDescs = new ArrayList<>(16);
		//内容变更后
		List<String> afterUpdateDescs = new ArrayList<>(16);
		if (!Convert.toStr(oldBuildingContractPlus.getContractNo(), "").equals(buildingContractPlus.getContractNo())) {
			befoerUpdateDescs.add("合同编码:" + oldBuildingContractPlus.getContractNo());
			afterUpdateDescs.add("合同编码:" + buildingContractPlus.getContractNo());
		}


		if (!oldBuildingContractPlus.getContractSource().equals(buildingContractPlus.getContractSource())) {
			befoerUpdateDescs.add("合同来源:" + getContractSourceName(oldBuildingContractPlus.getContractSource()));
			afterUpdateDescs.add("合同来源:" + getContractSourceName(buildingContractPlus.getContractSource()));
		}

		if (!oldBuildingContractPlus.getContractType().equals(buildingContractPlus.getContractType())) {
			befoerUpdateDescs.add("合同类型:" + getContractTypeName(oldBuildingContractPlus.getContractType()));
			afterUpdateDescs.add("合同类型:" + getContractTypeName(buildingContractPlus.getContractType()));
		}

		if (!oldBuildingContractPlus.getMaintainMainUserName().equals(buildingContractPlus.getMaintainMainUserName())) {
			befoerUpdateDescs.add("签约人:" + oldBuildingContractPlus.getMaintainMainUserName());
			afterUpdateDescs.add("签约人:" + buildingContractPlus.getMaintainMainUserName());
		}

		if (!oldBuildingContractPlus.getCustomerUnit().equals(buildingContractPlus.getCustomerUnit())) {
			befoerUpdateDescs.add("使用单位:" + oldBuildingContractPlus.getCustomerUnit());
			afterUpdateDescs.add("使用单位:" + buildingContractPlus.getCustomerUnit());
		}

		if (!oldBuildingContractPlus.getCustomerPhone().equals(buildingContractPlus.getCustomerPhone())) {
			befoerUpdateDescs.add("客户电话:" + oldBuildingContractPlus.getCustomerPhone());
			afterUpdateDescs.add("客户电话:" + buildingContractPlus.getCustomerPhone());
		}
		if (!oldBuildingContractPlus.getMaintainUnit().equals(buildingContractPlus.getMaintainUnit())) {
			befoerUpdateDescs.add("维保单位:" + oldBuildingContractPlus.getMaintainUnit());
			afterUpdateDescs.add("维保单位:" + buildingContractPlus.getMaintainUnit());
		}

		if (!oldBuildingContractPlus.getFromType().equals(buildingContractPlus.getFromType())) {
			befoerUpdateDescs.add("是否外包:" + (oldBuildingContractPlus.getFromType() == 1 ? "是" : "否"));
			afterUpdateDescs.add("是否外包:" + (buildingContractPlus.getFromType() == 1 ? "是" : "否"));
		}

		if (!oldBuildingContractPlus.getIsQuaGuarantee().equals(buildingContractPlus.getIsQuaGuarantee())) {
			befoerUpdateDescs.add("是否质保:" + (oldBuildingContractPlus.getIsQuaGuarantee() == 1 ? "是" : "否"));
			afterUpdateDescs.add("是否质保:" + (buildingContractPlus.getIsQuaGuarantee() == 1 ? "是" : "否"));
		}

		if (oldBuildingContractPlus.getContractBeginTime().getTime() != buildingContractPlus.getContractBeginTime().getTime() || oldBuildingContractPlus.getContractEndTime().getTime() != buildingContractPlus.getContractEndTime().getTime()) {
			befoerUpdateDescs.add("合同时间:" + DateUtil.format(oldBuildingContractPlus.getContractBeginTime(), "yyyy-MM-dd HH:mm:ss") + "-" + DateUtil.format(oldBuildingContractPlus.getContractEndTime(), "yyyy-MM-dd HH:mm:ss"));
			afterUpdateDescs.add("合同时间:" + DateUtil.format(buildingContractPlus.getContractBeginTime(), "yyyy-MM-dd HH:mm:ss") + "-" + DateUtil.format(buildingContractPlus.getContractEndTime(), "yyyy-MM-dd HH:mm:ss"));
		}

		if (!oldBuildingContractPlus.getContractCost().equals(buildingContractPlus.getContractCost())) {
			befoerUpdateDescs.add("合同金额:" + oldBuildingContractPlus.getContractCost());
			afterUpdateDescs.add("合同金额:" + buildingContractPlus.getContractCost());
		}

		if (!oldBuildingContractPlus.getPayType().equals(buildingContractPlus.getPayType())) {
			befoerUpdateDescs.add("支付方式:" + getPayTypeName(oldBuildingContractPlus.getPayType()));
			afterUpdateDescs.add("支付方式:" + getPayTypeName(buildingContractPlus.getPayType()));
		}

		if (!oldBuildingContractPlus.getInvoiceType().equals(buildingContractPlus.getInvoiceType())) {
			befoerUpdateDescs.add("票据类型:" + getInvoiceTypeName(oldBuildingContractPlus.getInvoiceType()));
			afterUpdateDescs.add("票据类型:" + getInvoiceTypeName(buildingContractPlus.getInvoiceType()));
		}

		if (!Convert.toInt(oldBuildingContractPlus.getPayDateType(), -1).equals(Convert.toInt(buildingContractPlus.getPayDateType(), -1))) {
			befoerUpdateDescs.add("付款周期-方式:" + getPayDateTypeName(oldBuildingContractPlus.getPayDateType()));
			afterUpdateDescs.add("付款周期-方式:" + getPayDateTypeName(buildingContractPlus.getPayDateType()));
		}

		if (!oldBuildingContractPlus.getPayDateDesc().equals(buildingContractPlus.getPayDateDesc())) {
			befoerUpdateDescs.add("付款周期:" + getpayDateDescName(oldBuildingContractPlus.getPayDateDesc()));
			afterUpdateDescs.add("付款周期:" + getpayDateDescName(buildingContractPlus.getPayDateDesc()));
		}

		//服务内容及方式要求
		BuildingContractExtPlus oldBuildingContractExtPlus = oldBuildingContractPlus.getBuildingContractExtPlus();
		if (oldBuildingContractExtPlus == null) {
			oldBuildingContractExtPlus = new BuildingContractExtPlus();
			oldBuildingContractExtPlus.setStagnationPoint(0);
			oldBuildingContractExtPlus.setMakeAppointment(0);
			oldBuildingContractExtPlus.setMaintainPersonnel(0);
			oldBuildingContractExtPlus.setRescueTime(0);
			oldBuildingContractExtPlus.setAllowMaintain(0);
			oldBuildingContractExtPlus.setAllowMaintainImm(0);
			oldBuildingContractExtPlus.setMalfunctionWorkTimeType(0);
			oldBuildingContractExtPlus.setInsureType(0);
		}


		BuildingContractExtPlus buildingContractExtPlus = buildingContractPlus.getBuildingContractExtPlus();
		if (!oldBuildingContractExtPlus.getStagnationPoint().equals(buildingContractExtPlus.getStagnationPoint())) {
			befoerUpdateDescs.add("是否驻点:" + (oldBuildingContractExtPlus.getStagnationPoint() == 1 ? "不限" : "驻点"));
			afterUpdateDescs.add("是否驻点:" + (buildingContractExtPlus.getStagnationPoint() == 1 ? "不限" : "驻点"));
		}

		if (!oldBuildingContractExtPlus.getMakeAppointment().equals(buildingContractExtPlus.getMakeAppointment())) {
			befoerUpdateDescs.add("预约要求:" + (oldBuildingContractExtPlus.getMakeAppointment() == 0 ? "不限" : "需要"));
			afterUpdateDescs.add("预约要求:" + (buildingContractExtPlus.getMakeAppointment() == 0 ? "不限" : "需要"));
		}

		if (!oldBuildingContractExtPlus.getMaintainPersonnel().equals(buildingContractExtPlus.getMaintainPersonnel())) {
			befoerUpdateDescs.add("保养人数:" + getMaintainPersonnelName(oldBuildingContractExtPlus.getMaintainPersonnel()));
			afterUpdateDescs.add("保养人数:" + getMaintainPersonnelName(buildingContractExtPlus.getMaintainPersonnel()));
		}

		//救援抵达要求
		if (!oldBuildingContractExtPlus.getRescueTime().equals(buildingContractExtPlus.getRescueTime())) {
			if (oldBuildingContractExtPlus.getRescueTime() == 0) {
				befoerUpdateDescs.add("救援抵达要求:30分钟内");
				afterUpdateDescs.add("救援抵达要求:白天:" + (buildingContractExtPlus.getRescueDaytime() + "分钟内抵达救援现场") + "-晚上:" + (buildingContractExtPlus.getRescueNight() + "分钟内抵达救援现场"));
			} else {
				befoerUpdateDescs.add("救援抵达要求:白天:" + (oldBuildingContractExtPlus.getRescueDaytime() + "分钟内抵达救援现场") + "-晚上:" + (oldBuildingContractExtPlus.getRescueNight() + "分钟内抵达救援现场"));
				afterUpdateDescs.add("救援抵达要求:30分钟内");
			}

		} else if (oldBuildingContractExtPlus.getRescueTime() == 1 && buildingContractExtPlus.getRescueTime() == 1) {
			if (!oldBuildingContractExtPlus.getRescueDaytime().equals(buildingContractExtPlus.getRescueDaytime()) || !oldBuildingContractExtPlus.getRescueNight().equals(buildingContractExtPlus.getRescueNight())) {
				befoerUpdateDescs.add("救援抵达要求:白天:" + (oldBuildingContractExtPlus.getRescueDaytime() + "分钟内抵达救援现场") + "-晚上:" + (oldBuildingContractExtPlus.getRescueNight() + "分钟内抵达救援现场"));
				afterUpdateDescs.add("救援抵达要求:白天:" + (buildingContractExtPlus.getRescueDaytime() + "分钟内抵达救援现场") + "-晚上:" + (buildingContractExtPlus.getRescueNight() + "分钟内抵达救援现场"));
			}
		}
		//允许保养作业时间
		if (!oldBuildingContractExtPlus.getAllowMaintain().equals(buildingContractExtPlus.getAllowMaintain())) {
			if (oldBuildingContractExtPlus.getAllowMaintain() == 0) {
				befoerUpdateDescs.add("允许保养作业时间:不限");
				List<MaintainTimeSubmitDTO> maintainTimeList = buildingContractExtPlus.getBuildingMaintainTimes();
				maintainTimeList = maintainTimeList.stream().sorted(Comparator.comparing(MaintainTimeSubmitDTO::getWeek)).collect(Collectors.toList());
				afterUpdateDescs.add("允许保养作业时间可作业时间段:");
				for (MaintainTimeSubmitDTO buildingContractMaintainTime : maintainTimeList) {
					if (buildingContractMaintainTime.getType() == 0) {
						afterUpdateDescs.add("<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-不限");
					}
					if (buildingContractMaintainTime.getType() == 1) {
						String str = "<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-可作业时间段:";
						List<MaintainTimeDTO> maintainTimeList1 = buildingContractMaintainTime.getMaintainTimeList();
						String str1 = "-";
						for (MaintainTimeDTO maintainTimeDTO : maintainTimeList1) {
							str1 = str1 + ";" + (maintainTimeDTO.getStartTime() + "-" + maintainTimeDTO.getEndTime());

						}
						afterUpdateDescs.add(str + str1);
					}
				}
			} else {
				afterUpdateDescs.add("允许保养作业时间:不限");
				List<MaintainTimeSubmitDTO> maintainTimeList = oldBuildingContractExtPlus.getBuildingMaintainTimes();
				maintainTimeList = maintainTimeList.stream().sorted(Comparator.comparing(MaintainTimeSubmitDTO::getWeek)).collect(Collectors.toList());
				befoerUpdateDescs.add("允许保养作业时间可作业时间段:");
				for (MaintainTimeSubmitDTO buildingContractMaintainTime : maintainTimeList) {
					if (buildingContractMaintainTime.getType() == 0) {
						befoerUpdateDescs.add("<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-不限");
					}
					if (buildingContractMaintainTime.getType() == 1) {
						String str = "<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-可作业时间段:";
						List<MaintainTimeDTO> maintainTimeList1 = buildingContractMaintainTime.getMaintainTimeList();
						String str1 = "-";
						for (MaintainTimeDTO maintainTimeDTO : maintainTimeList1) {
							str1 = str1 + ";" + (maintainTimeDTO.getStartTime() + "-" + maintainTimeDTO.getEndTime());
						}
						befoerUpdateDescs.add(str + str1);
					}
				}
			}
		} else if (oldBuildingContractExtPlus.getAllowMaintain() == 1 && buildingContractExtPlus.getAllowMaintain() == 1) {
			List<MaintainTimeSubmitDTO> timeSubmitDTOList = oldBuildingContractExtPlus.getBuildingMaintainTimes();
			for (MaintainTimeSubmitDTO maintainTimeSubmitDTO : timeSubmitDTOList) {
				Integer type1 = maintainTimeSubmitDTO.getType();
				if (type1 == 0) {
					List<MaintainTimeDTO> maintainTimeLists = new ArrayList<>(1);
					MaintainTimeDTO maintainTimeDTO = new MaintainTimeDTO();
					maintainTimeDTO.setEndTime(null);
					maintainTimeDTO.setStartTime(null);
					maintainTimeLists.add(maintainTimeDTO);
					maintainTimeSubmitDTO.setMaintainTimeList(maintainTimeLists);
				}
			}
			if (!JSON.toJSONString(oldBuildingContractExtPlus.getBuildingMaintainTimes()).equals(JSON.toJSONString(buildingContractExtPlus.getBuildingMaintainTimes()))) {
				List<MaintainTimeSubmitDTO> maintainTimeList = oldBuildingContractExtPlus.getBuildingMaintainTimes();
				maintainTimeList = maintainTimeList.stream().sorted(Comparator.comparing(MaintainTimeSubmitDTO::getWeek)).collect(Collectors.toList());
				befoerUpdateDescs.add("允许保养作业时间可作业时间段:");
				for (MaintainTimeSubmitDTO buildingContractMaintainTime : maintainTimeList) {
					if (buildingContractMaintainTime.getType() == 0) {
						befoerUpdateDescs.add("<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-不限");
					}
					if (buildingContractMaintainTime.getType() == 1) {
						String str = "<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-可作业时间段:";
						List<MaintainTimeDTO> maintainTimeList1 = buildingContractMaintainTime.getMaintainTimeList();
						String str1 = ";";
						for (MaintainTimeDTO maintainTimeDTO : maintainTimeList1) {
							str1 = str1 + ";" + (maintainTimeDTO.getStartTime() + "-" + maintainTimeDTO.getEndTime());
						}
						befoerUpdateDescs.add(str + str1);
					}
				}

				List<MaintainTimeSubmitDTO> maintainTimeList2 = buildingContractExtPlus.getBuildingMaintainTimes();
				afterUpdateDescs.add("允许保养作业时间可作业时间段:");
				for (MaintainTimeSubmitDTO buildingContractMaintainTime : maintainTimeList2) {
					if (buildingContractMaintainTime.getType() == 0) {
						afterUpdateDescs.add("<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-不限");
					}
					if (buildingContractMaintainTime.getType() == 1) {
						String str = "<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-可作业时间段:";
						List<MaintainTimeDTO> maintainTimeList1 = buildingContractMaintainTime.getMaintainTimeList();
						String str1 = "-";
						for (MaintainTimeDTO maintainTimeDTO : maintainTimeList1) {
							str1 = str1 + ";" + (maintainTimeDTO.getStartTime() + "-" + maintainTimeDTO.getEndTime());
						}
						afterUpdateDescs.add(str + str1);
					}
				}
			}
		}
		//故障特殊要求
		if (!oldBuildingContractExtPlus.getMalfunctionWorkTimeType().equals(buildingContractExtPlus.getMalfunctionWorkTimeType())) {

			if (oldBuildingContractExtPlus.getMalfunctionWorkTimeType() == 0) {
				befoerUpdateDescs.add("故障特殊要求:不限");
				afterUpdateDescs.add("故障特殊要求-急修到达时间:" + buildingContractExtPlus.getMalfunctionWorkArrivalTime() + "-恢复时间:" + buildingContractExtPlus.getMalfunctionWorkRenewTime());
			} else {
				befoerUpdateDescs.add("故障特殊要求-急修到达时间:" + oldBuildingContractExtPlus.getMalfunctionWorkArrivalTime() + "-恢复时间:" + oldBuildingContractExtPlus.getMalfunctionWorkRenewTime());
				afterUpdateDescs.add("故障特殊要求:不限");
			}
		} else if (oldBuildingContractExtPlus.getMalfunctionWorkTimeType() == 1 && buildingContractExtPlus.getMalfunctionWorkTimeType() == 1) {
			if (!oldBuildingContractExtPlus.getMalfunctionWorkArrivalTime().equals(buildingContractExtPlus.getMalfunctionWorkArrivalTime()) || !oldBuildingContractExtPlus.getMalfunctionWorkRenewTime().equals(buildingContractExtPlus.getMalfunctionWorkRenewTime())) {
				befoerUpdateDescs.add("故障特殊要求-急修到达时间:" + oldBuildingContractExtPlus.getMalfunctionWorkArrivalTime() + "-恢复时间:" + oldBuildingContractExtPlus.getMalfunctionWorkRenewTime());
				afterUpdateDescs.add("故障特殊要求-急修到达时间:" + buildingContractExtPlus.getMalfunctionWorkArrivalTime() + "-恢复时间:" + buildingContractExtPlus.getMalfunctionWorkRenewTime());
			}
		}

		//是否购买电梯责任险
		if (!oldBuildingContractExtPlus.getInsureType().equals(buildingContractExtPlus.getInsureType())) {
			if (oldBuildingContractExtPlus.getInsureType() == 0) {
				befoerUpdateDescs.add("是否购买电梯责任险:否");
				afterUpdateDescs.add("购买电梯责任险:每次事故责任限额:" + Convert.toInt(buildingContractExtPlus.getInsureAmountCount(), 0) + "万" + "-每人责任限额:" + Convert.toInt(buildingContractExtPlus.getInsureAmountUser(), 0) + "万");
			} else {
				befoerUpdateDescs.add("购买电梯责任险:每次事故责任限额:" + oldBuildingContractExtPlus.getInsureAmountCount() + "万" + "-每人责任限额:" + buildingContractExtPlus.getInsureAmountUser() + "万");
				afterUpdateDescs.add("是否购买电梯责任险:否");
			}
		} else if (oldBuildingContractExtPlus.getInsureType() == 1 && buildingContractExtPlus.getInsureType() == 1) {
			if (!Convert.toInt(oldBuildingContractExtPlus.getInsureAmountCount(), 0).equals(Convert.toInt(buildingContractExtPlus.getInsureAmountCount(), 0)) || !Convert.toInt(oldBuildingContractExtPlus.getInsureAmountUser(), 0).equals(Convert.toInt(buildingContractExtPlus.getInsureAmountUser(), 0))) {
				befoerUpdateDescs.add("购买电梯责任险:每次事故责任限额:" + Convert.toInt(oldBuildingContractExtPlus.getInsureAmountCount(), 0) + "万" + "-每人责任限额:" + Convert.toInt(oldBuildingContractExtPlus.getInsureAmountUser(), 0) + "万");
				afterUpdateDescs.add("购买电梯责任险:每次事故责任限额:" + Convert.toInt(buildingContractExtPlus.getInsureAmountCount(), 0) + "万" + "-每人责任限额:" + Convert.toInt(buildingContractExtPlus.getInsureAmountUser(), 0) + "万");
			}
		}
		//处理项目数据
		List<ContractBuildingInfoVo> oldBuildingInfos = oldBuildingContractPlus.getBuildingInfo();
		List<Long> buildingIds = oldBuildingInfos.stream().map(ContractBuildingInfoVo::getBuildingId).distinct().collect(Collectors.toList());
		Map<Long, List<ContractBuildingInfoVo>> map = oldBuildingInfos.stream().collect(Collectors.groupingBy(ContractBuildingInfoVo::getBuildingId));
		for (Long buildingId : buildingIds) {
			List<ContractBuildingInfoVo> contractBuildingInfoVos = map.get(Convert.toLong(buildingId));
			ContractBuildingInfoVo contractBuildingInfoVo1 = contractBuildingInfoVos.get(0);
			contractBuildingInfoVo1.setElevatorCounts(contractBuildingInfoVos.size());
		}
		List<ContractBuildingInfoVo> buildingInfos = buildingContractPlus.getBuildingInfo();
		if (!JSON.toJSONString(oldBuildingInfos).equals(JSON.toJSONString(buildingInfos))) {
			String afterBuilding = "合同项目电梯及维保费用:<br/>";
			afterUpdateDescs.add(afterBuilding);
			for (ContractBuildingInfoVo buildingInfoVo : buildingInfos) {
				String str = "";
				if (StrUtil.isNotEmpty(buildingInfoVo.getBuildingName())) {
					str = str + "<span style=margin-left:12px;></span>项目:" + buildingInfoVo.getBuildingName();
				}
				if (StrUtil.isNotEmpty(buildingInfoVo.getElevatorAddress())) {
					str = str + "-电梯:" + buildingInfoVo.getElevatorAddress();
				}
				if (StrUtil.isNotEmpty(buildingInfoVo.getMonthCostDesc())) {
					str = str + "-月保养费用:" + buildingInfoVo.getMonthCostDesc();
				}
				if (buildingInfoVo.getMaintainCounts() != null) {
					str = str + "-服务月数(月):" + buildingInfoVo.getMaintainCounts();
				}
				if (buildingInfoVo.getServeCounts() != null) {
					str = str + "-服务次数:" + buildingInfoVo.getServeCounts();
				}

				MaintainContractType contractTypeHistoryInfo = maintainContractTypeService.getById(buildingInfoVo.getMaintainType());
				if (contractTypeHistoryInfo != null) {
					if (StrUtil.isNotEmpty(contractTypeHistoryInfo.getName())) {
						str = str + "-保养类型:" + contractTypeHistoryInfo.getName();
					}
				}
				if (buildingInfoVo.getYearCostType() != null) {
					str = str + "-年检费用合计:" + getYearSpeedGovernorCostTypeName(buildingInfoVo.getYearCostType()) + "=" + buildingInfoVo.getYearCostDesc() + "-限速器年检费(元)" +
						getYearSpeedGovernorCostTypeName(buildingInfoVo.getYearSpeedGovernorCostType()) + "=" + buildingInfoVo.getYearSpeedGovernorCost();
				}
				if (buildingInfoVo.getExtUrls() != null && buildingInfoVo.getExtUrls().size() > 0) {
					str = str + "-<div style='display: none;'>&配件清单:" + JSON.toJSONString(buildingInfoVo.getExtUrls()) + "&</div>";
				} else {
					str = str + "-&配件清单:-&";
				}
				afterUpdateDescs.add(str);
			}


			String befoerBuilding = "合同项目电梯及维保费用:<br/>";
			befoerUpdateDescs.add(befoerBuilding);
			for (ContractBuildingInfoVo buildingInfoVo : oldBuildingInfos) {
				String str = "";
				if (StrUtil.isNotEmpty(buildingInfoVo.getBuildingName())) {
					str = str + "<span style=margin-left:12px;></span>项目:" + buildingInfoVo.getBuildingName();
				}
				if (StrUtil.isNotEmpty(buildingInfoVo.getElevatorAddress())) {
					str = str + "-电梯:" + buildingInfoVo.getElevatorAddress();
				}
				if (StrUtil.isNotEmpty(buildingInfoVo.getMonthCostDesc())) {
					str = str + "-月保养费用:" + buildingInfoVo.getMonthCostDesc();
				}
				if (buildingInfoVo.getMaintainCounts() != null) {
					str = str + "-服务月数(月):" + buildingInfoVo.getMaintainCounts();
				}

				if (buildingInfoVo.getMaintainCounts() != null) {
					str = str + "-服务月数(月):" + buildingInfoVo.getMaintainCounts();
				}
				MaintainContractType contractTypeHistoryInfo = maintainContractTypeService.getById(buildingInfoVo.getMaintainType());
				if (contractTypeHistoryInfo != null) {
					if (StrUtil.isNotEmpty(contractTypeHistoryInfo.getName())) {
						str = str + "-保养类型:" + contractTypeHistoryInfo.getName();
					}
				}
				if (buildingInfoVo.getYearCostType() != null) {
					str = str + "-年检费用合计:" + getYearSpeedGovernorCostTypeName(buildingInfoVo.getYearCostType()) + "=" + buildingInfoVo.getYearCostDesc() + "-限速器年检费(元)" +
						getYearSpeedGovernorCostTypeName(buildingInfoVo.getYearSpeedGovernorCostType()) + "=" + buildingInfoVo.getYearSpeedGovernorCost();
				}
				if (buildingInfoVo.getExtUrls() != null && buildingInfoVo.getExtUrls().size() > 0) {
					str = str + "-<div style='display: none;'>&配件清单:" + JSON.toJSONString(buildingInfoVo.getExtUrls()) + "&</div>";
				} else {
					str = str + "-&配件清单:-&";
				}
				befoerUpdateDescs.add(str);
			}

		}


		if (!JSON.toJSONString(buildingContractPlus.getContractExt()).equals(JSON.toJSONString(oldBuildingContractPlus.getContractExt()))) {
			befoerUpdateDescs.add("合同附件:\n" + JSON.toJSONString(oldBuildingContractPlus.getContractExt()));
			afterUpdateDescs.add("合同附件:\n" + JSON.toJSONString(buildingContractPlus.getContractExt()));
		}


		if (!Convert.toStr(oldBuildingContractPlus.getContractDesc(), "").equals(Convert.toStr(buildingContractPlus.getContractDesc(), ""))) {
			befoerUpdateDescs.add("备注:" + Convert.toStr(oldBuildingContractPlus.getContractDesc(), "-"));
			afterUpdateDescs.add("备注:" + Convert.toStr(buildingContractPlus.getContractDesc(), "-"));
		}

		if (befoerUpdateDescs.size() == 0) {
			befoerUpdateDescs.add("仅合同操作-无内容变更");
		}
		if (afterUpdateDescs.size() == 0) {
			afterUpdateDescs.add("仅合同操作-无内容变更");
		}
		beforeDesc.setUpdateDescs(befoerUpdateDescs);
		afterDesc.setUpdateDescs(afterUpdateDescs);
		contractLog.setBeforeDesc(beforeDesc);
		contractLog.setAfterDesc(afterDesc);
		this.save(contractLog);
	}


	@Override
	public ContractUpdateDescVo addContractSaveLog(BuildingContractPlus buildingContractPlus) {
		if (StrUtil.isEmpty(buildingContractPlus.getMaintainMainUserName())) {
			User user = userService.getById(buildingContractPlus.getMaintainMainUser());
			if (user != null) {
				buildingContractPlus.setMaintainMainUserName(user.getRealName());
			}
		}
		ContractUpdateDescVo afterDesc = new ContractUpdateDescVo();
		//内容变更后
		List<String> afterUpdateDescs = new ArrayList<>(16);
		afterUpdateDescs.add("合同编码:" + buildingContractPlus.getContractNo());
		afterUpdateDescs.add("合同来源:" + getContractSourceName(buildingContractPlus.getContractSource()));
		afterUpdateDescs.add("合同类型:" + getContractTypeName(buildingContractPlus.getContractType()));
		afterUpdateDescs.add("签约人:" + buildingContractPlus.getMaintainMainUserName());
		afterUpdateDescs.add("使用单位:" + buildingContractPlus.getCustomerUnit());
		afterUpdateDescs.add("客户电话:" + buildingContractPlus.getCustomerPhone());
		afterUpdateDescs.add("维保单位:" + buildingContractPlus.getMaintainUnit());
		afterUpdateDescs.add("是否外包:" + (buildingContractPlus.getFromType() == 1 ? "是" : "否"));
		afterUpdateDescs.add("是否质保:" + (buildingContractPlus.getIsQuaGuarantee() == 1 ? "是" : "否"));
		afterUpdateDescs.add("合同时间:" + DateUtil.format(buildingContractPlus.getContractBeginTime(), "yyyy-MM-dd HH:mm:ss") + "~" + DateUtil.format(buildingContractPlus.getContractEndTime(), "yyyy-MM-dd HH:mm:ss"));
		afterUpdateDescs.add("合同金额:" + buildingContractPlus.getContractCost());
		afterUpdateDescs.add("支付方式:" + getPayTypeName(buildingContractPlus.getPayType()));
		afterUpdateDescs.add("票据类型:" + getInvoiceTypeName(buildingContractPlus.getInvoiceType()));
		afterUpdateDescs.add("付款周期:<br/>");
		afterUpdateDescs.add("<span style=margin-left:12px;></span>时间:" + getPayDateTypeName(buildingContractPlus.getPayDateType()) + "<br/>");
		afterUpdateDescs.add("<span style=margin-left:12px;></span>方式:" + getpayDateDescName(buildingContractPlus.getPayDateDesc()));
		//服务内容及方式要求
		BuildingContractExtPlus buildingContractExtPlus = buildingContractPlus.getBuildingContractExtPlus();
		afterUpdateDescs.add("是否驻点:" + (buildingContractExtPlus.getStagnationPoint() == 0 ? "不限" : "驻点"));
		afterUpdateDescs.add("预约要求:" + (buildingContractExtPlus.getMakeAppointment() == 0 ? "不限" : "需要"));
		afterUpdateDescs.add("保养人数:" + getMaintainPersonnelName(buildingContractExtPlus.getMaintainPersonnel()));
		if (buildingContractExtPlus.getRescueTime() == 0) {
			afterUpdateDescs.add("救援抵达要求:30分钟内" + (buildingContractExtPlus.getRescueTime() == 0 ? "" : ""));
		}
		if (buildingContractExtPlus.getRescueTime() == 1) {
			afterUpdateDescs.add("特殊救援时间要求-白天:" + (buildingContractExtPlus.getRescueDaytime() + "分钟内抵达救援现场") + "-晚上:" + (buildingContractExtPlus.getRescueNight() + "分钟内抵达救援现场"));
		}
		if (buildingContractExtPlus.getAllowMaintain() == 0) {
			afterUpdateDescs.add("允许保养作业时间:不限");
		}
		if (buildingContractExtPlus.getAllowMaintain() == 1) {
			List<MaintainTimeSubmitDTO> maintainTimeList = buildingContractExtPlus.getBuildingMaintainTimes();
			maintainTimeList = maintainTimeList.stream().sorted(Comparator.comparing(MaintainTimeSubmitDTO::getWeek)).collect(Collectors.toList());
			afterUpdateDescs.add("允许保养作业时间可作业时间段:");
			for (MaintainTimeSubmitDTO buildingContractMaintainTime : maintainTimeList) {
				if (buildingContractMaintainTime.getType() == 0) {
					afterUpdateDescs.add("<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-不限");
				}
				if (buildingContractMaintainTime.getType() == 1) {
					String str = "<span style=margin-left:12px;></span>周" + buildingContractMaintainTime.getWeek() + "-可作业时间段:";
					List<MaintainTimeDTO> maintainTimeList1 = buildingContractMaintainTime.getMaintainTimeList();
					String str1 = "-";
					for (MaintainTimeDTO maintainTimeDTO : maintainTimeList1) {
						str1 = str1 + ";" + (maintainTimeDTO.getStartTime() + ";-" + maintainTimeDTO.getEndTime());

					}
					afterUpdateDescs.add(str + str1);
				}
			}
		}
		if (buildingContractExtPlus.getMalfunctionWorkTimeType() == 0) {
			afterUpdateDescs.add("故障特殊要求:不限");
		}
		if (buildingContractExtPlus.getMalfunctionWorkTimeType() == 1) {
			afterUpdateDescs.add("故障特殊要求-急修到达时间:" + buildingContractExtPlus.getMalfunctionWorkArrivalTime() + "--恢复时间:" + buildingContractExtPlus.getMalfunctionWorkRenewTime());
		}
		if (buildingContractExtPlus.getInsureType() == 0) {
			afterUpdateDescs.add("是否购买电梯责任险:否");
		}
		if (buildingContractExtPlus.getInsureType() == 1) {
			afterUpdateDescs.add("购买电梯责任险-每次事故责任限额:" + Convert.toInt(buildingContractExtPlus.getInsureAmountCount(), 0) + "万" + "--每人责任限额:" + buildingContractExtPlus.getInsureAmountUser() + "万");
		}

		//处理项目数据
		List<ContractBuildingInfoVo> buildingInfos = buildingContractPlus.getBuildingInfo();
		String afterBuilding = "合同项目电梯及维保费用:<br/>";
		afterUpdateDescs.add(afterBuilding);
		for (ContractBuildingInfoVo buildingInfoVo : buildingInfos) {
			String str = "";
			if (StrUtil.isNotEmpty(buildingInfoVo.getBuildingName())) {
				str = str + "<span style=margin-left:12px;></span>项目:" + buildingInfoVo.getBuildingName();
			}
			if (StrUtil.isNotEmpty(buildingInfoVo.getElevatorAddress())) {
				str = str + "-电梯:" + buildingInfoVo.getElevatorAddress();
			}
			if (StrUtil.isNotEmpty(buildingInfoVo.getMonthCostDesc())) {
				str = str + "-月保养费用:" + buildingInfoVo.getMonthCostDesc();
			}
			if (buildingInfoVo.getMaintainCounts() != null) {
				str = str + "-服务月数(月):" + buildingInfoVo.getMaintainCounts();
			}

			MaintainContractType contractTypeHistoryInfo = maintainContractTypeService.getById(buildingInfoVo.getMaintainType());
			if (contractTypeHistoryInfo != null) {
				if (StrUtil.isNotEmpty(contractTypeHistoryInfo.getName())) {
					str = str + "-保养类型:" + contractTypeHistoryInfo.getName();
				}
			}
			if (buildingInfoVo.getYearCostType() != null) {
				str = str + "-年检费用合计:" + getYearSpeedGovernorCostTypeName(buildingInfoVo.getYearCostType()) + "=" + buildingInfoVo.getYearCostDesc() + "-限速器年检费(元)" +
					getYearSpeedGovernorCostTypeName(buildingInfoVo.getYearSpeedGovernorCostType()) + "=" + buildingInfoVo.getYearSpeedGovernorCost();
			}
			if (buildingInfoVo.getExtUrls() != null && buildingInfoVo.getExtUrls().size() > 0) {
				str = str + "-<div style='display: none;'>&配件清单:" + JSON.toJSONString(buildingInfoVo.getExtUrls()) + "&</div>";
			} else {
				str = str + "-&配件清单:-&";
			}
			afterUpdateDescs.add(str);
		}
		afterUpdateDescs.add("备注:" + Convert.toStr(buildingContractPlus.getContractDesc(), "-"));

		if (buildingContractPlus.getContractExt() != null && buildingContractPlus.getContractExt().size() > 0) {
			afterUpdateDescs.add("合同附件:" + JSON.toJSONString(buildingContractPlus.getContractExt()));
		} else {
			afterUpdateDescs.add("合同附件:-");
		}
		afterDesc.setUpdateDescs(afterUpdateDescs);
		return afterDesc;
	}

	/**
	 * 封装项目和电梯 为字符串
	 *
	 * @return
	 */
	private String packagingBuildAndEle(BuildingContractPlus buildingContractPlus) {
		List<String> buildingNames = new ArrayList<>();
		if (buildingContractPlus.getBuildingInfo() != null && buildingContractPlus.getBuildingInfo().size() > 0) {
			List<ContractBuildingInfoVo> buildingInfo = buildingContractPlus.getBuildingInfo();
			Map<Long, List<ContractBuildingInfoVo>> listMap = buildingInfo.stream().collect(Collectors.groupingBy(ContractBuildingInfoVo::getBuildingId));
			List<Long> buildingIds = buildingInfo.stream().map(ContractBuildingInfoVo::getBuildingId).distinct().collect(Collectors.toList());
			for (Long buildingId : buildingIds) {
				List<ContractBuildingInfoVo> contractBuildingInfoVos = listMap.get(buildingId);
				String buildingName = contractBuildingInfoVos.get(0).getBuildingName();
				List<String> eleNames = contractBuildingInfoVos.stream().map(ContractBuildingInfoVo::getElevatorAddress).collect(Collectors.toList());
				buildingName = buildingName + ":" + String.join("-", eleNames);
				buildingNames.add(buildingName);
			}
		}
		return String.join(",", buildingNames);
	}


}
